export default {
	// apiWebHost: '/hjzf', //测试
	apiWebHost:'https://hd.kotom.cn',//测试
	adminToken: "token",
	ImgUrl: "",
	HImgUrl: "",
	token: "",
	APIURL: function(action) {
		return this.apiWebHost + action;
	},
	ImgFile: function(id, w, h) {
		if (id) {
			var url = this.apiWebHost + id;
			return url;
		}
	},
	downloadFunc: (param) => {
		const {
			method = 'get', url = '', target = '', data = {}
		} = param
		param.type = param.type || 'download';
		let str = ''
		for (const key in data) {
			str += `<input name="${key}" value="${data[key]}" type="hidden" />`
		}
		const form = new Blob('form');
		form.action = param.type == 'download' ? process.env.VUE_APP_BASE_API + url : url;
		form.method = method;
		form.target = target;
		form.style.display = 'none'
		document.body.appendChild(form)
		form.innerHTML = str
		form.submit()
		document.body.removeChild(form)
	},
	// 记录缓存
	setCookie: function(name, value) { //设置cookie，默认保存7天
		uni.setStorage({
			key: name,
			data: value,
			success: function() {
				console.log('success');
			}
		});
	},
	// 获取缓存
	getStorage: function(key, callback) {
		return new Promise((resolve) => {
			uni.getStorage({
				key: key,
				success: function(res) {
					resolve(res.data)
				},
				fail: function(res) {
					resolve(res.data)
				},
				complete: function(res) {
					resolve(res.data)
				},
			})
		})
	},
	// 上传
	upload(tempFilePaths) {
		return new Promise((resolve) => {
			let imgList = [];
			tempFilePaths.forEach((item, index) => {
				console.log(item)
				uni.uploadFile({
					url: this.APIURL('/rest/api/upload'), //仅为示例，非真实的接口地址
					filePath: item,
					header: {
						'sessionId': this.token,
					},
					name: 'file',
					success: (uploadFileRes) => {
						let res = JSON.parse(uploadFileRes.data);
						if (res.code == 0) {
							imgList.push(res.url);
							if (index == tempFilePaths.length - 1) {
								resolve(imgList)
							}
						}
					},
					complete:(e)=>{
						console.log(e)
					}
				});
			})


		})
	},
	// 上传视频
	ChooseVideo(num) {
		return new Promise((resolve) => {
			uni.chooseVideo({
				compressed:false,
				maxDuration: 60, //拍摄视频最长拍摄时间，单位秒。最长支持 60 秒。
				camera: 'back', //'front'、'back'，默认'back'
				sourceType: ['album', 'camera'], //从相册选择
				success: (res) => {
					const tempFilePaths = res.tempFilePath;
					resolve(tempFilePaths)
				}
			})
		});
	},
	// 上传图片
	ChooseImage(num) {
		return new Promise((resolve) => {
			uni.chooseImage({
				count: num, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album', 'camera'], //从相册选择
				success: (res) => {
					const tempFilePaths = res.tempFilePaths;
					let uploadimg = this.upload(tempFilePaths);
					setTimeout(() => {
						uploadimg.then((result) => {
							resolve(result)
						})
					}, 1000);
				}
			})
		});
	},
	//删除缓存
	remove: function(name) {
		return new Promise((resolve) => {
			uni.removeStorage({
				key: name,
				success: function(res) {
					resolve(res)
				}
			})
		})
	},
	ajax: function(opt) {
		let _this = this;
		opt = opt || {};
		opt.isToken = opt.isToken == undefined ? true : opt.isToken;
		opt.url = !opt.type ? this.APIURL(opt.url) : opt.url;
		opt.data = opt.data || null;
		opt.method = opt.method || 'GET';
		opt.header = opt.header || {
			'sessionId': opt.isToken ? this.token : '',
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
			'Accept': 'application/json',
		};
		opt.success = opt.success || function() {};
		opt.complete = opt.complete || function() {};
		uni.request({
			url: opt.url,
			data: opt.data,
			method: opt.method,
			header: opt.header,
			dataType: 'json',
			success: function(res) {
				if (res.data.code == 401) {
					_this.remove(_this.adminToken)
					uni.reLaunch({
						url: '/pages/login/login?type=1'
					})
					return
				}
				if (res.data.code != 0) {
					uni.hideLoading();
					setTimeout(() => {
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 3000
						});
					}, 0)
				} else {
					opt.success(res.data);
				}
			},
			fail: function(res) {},
			complete(res) {
				opt.complete(res.data);
			}
		})
	},
	dateCalculation: function(data) { //返回天、时、分
		var newTime = date.replace(/\-/g, "/");
		let nowdata = new Date().getTime();
		let enddata = new Date(newTime).getTime();
		let second = parseInt(Math.abs(enddata - nowdata));
		console.log(second)
		if (second > 0) {
			let data = {};
			let day = parseInt(second / (1000 * 60 * 60 * 24));
			let hour = parseInt((second % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
			let minute = parseInt((second % (1000 * 60 * 60)) / (1000 * 60));
			let seconds = (second % (1000 * 60)) / 1000;
			data.day = day;
			data.hour = hour;
			data.minute = minute;
			data.seconds = seconds;
			return data;
		}
	},
	//格式化日期，format:格式，默认yyyy-MM-dd,date时间，默认当前时间
	getStatus: function(value) {
		let clock = ''
		//0：待付款，1：待发货,2：待收货,3:已收货，4：待成团，8：已退单，9：已取消 
		switch (value) {
			case 0: {
				clock = '待付款';
			}
			break;
		case 1: {
			clock = '待发货';
		}
		break;
		case 2: {
			clock = '待收货';
		}
		break;
		case 3: {
			clock = '已收货';
		}
		break;
		case 4: {
			clock = '待成团';
		}
		break;
		case 8: {
			clock = '已退单';
		}
		break;
		case 9: {
			clock = '已取消';
		}
		break;
		}
		return clock;

	},
	//格式化日期，format:格式，默认yyyy-MM-dd,date时间，默认当前时间
	getNowDate: function(format, date, type = 'String') {
		if (!format) {
			format = "yyyy-MM-dd";
		}
		var now = new Date();
		if (date) {
			var newTime = type == 'String' ? date.replace(/\-/g, "/") : date;
			now = new Date(newTime);
			console.log(now)
		}
		var year = now.getFullYear(); //年
		var month = now.getMonth() + 1; //月
		var day = now.getDate(); //日
		var hh = now.getHours();
		var mm = now.getMinutes();
		var ss = now.getSeconds();
		var clock = format != 'HH:mm:ss'&&format!='yyyy年MM月dd日' ? year + "-" : '';
		switch (format) {
			case "yyyy-MM-dd": {
				if (month < 10)
					clock += "0";
				clock += month + "-";
				if (day < 10) {
					clock += "0";
				}
				clock += day;
			}
			break;
			case "yyyy年MM月dd日": {
				clock += year + "年"; 
				if (month < 10)
					clock += "0";
				clock += month + "月";
				if (day < 10) {
					clock += "0";
				}
				clock += day + "日";
			}
			break;
		case "yyyy-MM": {
			if (month < 10)
				clock += "0";
			clock += month;
		}
		break;
		case "yyyy-MM-dd HH:mm": {
			if (month < 10)
				clock += "0";
			clock += month + "-";
			if (day < 10) {
				clock += "0";
			}
			clock += day + " ";

			if (hh < 10)
				clock += "0";
			clock += hh + ":";
			if (mm < 10)
				clock += "0";
			clock += mm + ":";
			if (ss < 10)
				clock += "0";
			clock += ss;
			clock = clock.substring(0, 16)
		}
		break;
		case "HH:mm:ss": {
			if (hh < 10)
				clock += "0";
			clock += hh + ":";
			if (mm < 10)
				clock += "0";
			clock += mm + ":";
			if (ss < 10)
				clock += "0";
			clock += ss;
			clock = clock.substring(0, 16)
		}
		break;
		case "yyyy-MM-ddTHH:mm": {
			if (month < 10)
				clock += "0";
			clock += month + "-";
			if (day < 10) {
				clock += "0";
			}
			clock += day + "T";

			if (hh < 10)
				clock += "0";
			clock += hh + ":";
			if (mm < 10)
				clock += "0";
			clock += mm + ":";
			if (ss < 10)
				clock += "0";
			clock += ss;
			clock = clock.substring(0, 16)
		}
		break;
		case "yyyy-MM-dd HH:mm:ss": {
			if (month < 10)
				clock += "0";
			clock += month + "-";
			if (day < 10) {
				clock += "0";
			}
			clock += day + " ";

			if (hh < 10)
				clock += "0";
			clock += hh + ":";
			if (mm < 10)
				clock += "0";
			clock += mm + ":";
			if (ss < 10)
				clock += "0";
			clock += ss;
		}
		break;
		case "yyyyMMddHHmmss": {
			clock = year;
			if (month < 10)
				clock += "0";
			clock += month;
			if (day < 10) {
				clock += "0";
			}
			clock += day;

			if (hh < 10)
				clock += "0";
			clock += hh;
			if (mm < 10)
				clock += "0";
			clock += mm;
			if (ss < 10)
				clock += "0";
			clock += ss;
		}
		break;
		case "MMddHHmm": {
			clock = "";
			var nowdata = new Date();
			var nowYear = nowdata.getFullYear();
			var nowMonth = nowdata.getMonth() + 1;
			var nowDay = nowdata.getDate();
			var nowdatastr = nowYear + nowMonth + nowDay;
			var datastr = year + month + day;
			if (nowdatastr == datastr) {
				clock += hh + ":";
				clock += mm;
			} else {
				clock += month + "-";
				clock += day + " ";
				clock += hh + ":";
				clock += mm;
			}
		}
		break;
		}

		return clock;

	},

}
